package com.rockcent.third.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.JsonResult;
import com.rockcent.common.common.RestDoing;
import com.rockcent.common.controller.BaseController;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.mall.lib.domain.IntegrationChannel;
import com.rockcent.mall.lib.dto.ThirdPartyUserInfoDto;
import com.rockcent.mall.lib.repository.IntegrationChannelRepository;
import com.rockcent.third.Routes;
import com.rockcent.third.helper.UserHelper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 对外提供的第三方鉴权接口
 * Created by zhangjh on 2017/1/10.
 */
@RestController
@RequestMapping(value = Routes.API_PREFIX)
public class ThirdPartyLoginController extends BaseController {
    private final Logger log = LoggerFactory.getLogger(ThirdPartyLoginController.class);
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private UserHelper userHelper;

    @Autowired
    private IntegrationChannelRepository integrationChannelRepository;
    /**
     * @api {post} /third/v2/cgi-bin/token 用户登录接口
     * @apiGroup LOGIN
     * @apiVersion 1.0.0
     *
     * @apiParam {String} appKey <code>必须参数</code> 第三方唯一凭证
     * @apiParam {String} appSecret <code>必须参数</code> 第三方唯一凭证密钥
     * @apiSuccessExample {json} Success-Response:
     * {
     *  "code": "1",
     *  "desc": "成功",
     *  "data": {
     *      "accessToken": "ACCESS_TOKEN",
     *      "expiresIn": "7200"
     *  }
     * }
     *
     *
     */
    @RequestMapping(value = Routes.CGI_BIN_TOKEN, method = RequestMethod.POST)
    public JsonResult userLogin(@RequestBody ThirdPartyUserInfoDto thirdPartyUserInfoDto, HttpServletRequest request, HttpServletResponse response) {
        RestDoing doing = jr -> {
            if (thirdPartyUserInfoDto == null) {
                jr.remind("信息有误", log);
                return;
            }
            if (StringUtils.isBlank(thirdPartyUserInfoDto.getAppSecret())) {
                jr.remind("密钥为空", log);
                return;
            }
            if (StringUtils.isBlank(thirdPartyUserInfoDto.getAppKey())) {
                jr.remind("凭证为空", log);
                return;
            }
            IntegrationChannel integrationChannel = integrationChannelRepository.findByAppKeyAndAppSecretAndIsDelete(thirdPartyUserInfoDto.getAppKey(), thirdPartyUserInfoDto.getAppSecret(), Whether.NO);
            if(null==integrationChannel){
                jr.remind("渠道未授权", log);
                return;
            }
            ThirdPartyUserInfoDto userInfoDto = userHelper.setSession(thirdPartyUserInfoDto, request, integrationChannel);
            jr.data = userInfoDto;
        };
        return doing.go(thirdPartyUserInfoDto, null, request, objectMapper, log);
    }
}
